package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.CouponActivateListAdapter;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.CouponActivateEntity;
import com.eboss.sbs.model.CouponActivateListResult;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.views.MyListView;
import com.mytools.tool.views.SegmentView;
import com.mytools.tool.views.SegmentView.onSegmentViewClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CouponActivateActivity extends BaseActivity implements onSegmentViewClickListener, OnItemClickListener {
	
	private PullToRefreshScrollView pullToRefreshScrollView;
	private SegmentView segView;//可以隐藏中间的按钮
	private MyListView listview;
	private CouponActivateListAdapter adapter;
	private List<CouponActivateEntity> datas;
	private EditText tv_search_key;
	private int state = 0;
	private int page = 1;
	private int rows = Config.ROWS;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_point);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "优惠券活动 ");
		
		tv_search_key = (EditText)findViewById(R.id.tv_search_key);
		tv_search_key.setHint("输入活动名称查询");
		
		pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pullToRefreshScrollView);
		pullToRefreshScrollView.setOnRefreshListener(new MyOnRefreshListener());
		pullToRefreshScrollView.setMode(Mode.BOTH);
		
		datas = new ArrayList<CouponActivateEntity>();
		listview = (MyListView)findViewById(R.id.listview);
		adapter = new CouponActivateListAdapter(this, datas);
		
		segView = (SegmentView)findViewById(R.id.segView);
		segView.setOnSegmentViewClickListener(this);
		listview.setOnItemClickListener(this);
		segView.setTextValues(new String[]{"全部","活动中","待开始","已结束"});
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.img_search).setOnClickListener(this);
		listview.setAdapter(adapter);
		
		getdata();
	}
	
	class MyOnRefreshListener implements OnRefreshListener2<ScrollView> {
		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			pullToRefreshScrollView.setMode(Mode.BOTH);
			String label = DateUtils.formatDateTime(mActivity, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
			refreshData();
		}
		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
			page++;
			getdata();
		}
	}
	
	private void refreshData(){
		datas.clear();
		page = 1;
		getdata();
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		params.put("state", state);
		String key = getTvText(R.id.tv_search_key);
		if(!StringUtil.isNull(key)){params.put("key", key);}
		else{params.put("key", "");}
		params.put("pagenum", page);
		params.put("pagesize", rows);
		
		API.http_post(this, API.COUPON_ACTIVATE_LIST, params, new HttpCallback<CouponActivateListResult>(this) {
			@Override
			public void onSuccess(int statusCode, CouponActivateListResult data) {
				if(data!=null){
					 List<CouponActivateEntity> tmp_datas = data.getActivities();
					 if(tmp_datas==null||tmp_datas.size()==0||tmp_datas.size()<rows){
						if(tmp_datas==null||tmp_datas.size()==0){
							toast("没有更多数据");
						}
						pullToRefreshScrollView.setMode(Mode.PULL_DOWN_TO_REFRESH);
					 }
					 if(tmp_datas!=null&&tmp_datas.size()>0){
						 datas.addAll(tmp_datas);
					 }
				}
				boolean no_datas = (datas==null||datas.size()==0);
				showView(R.id.eva_nodata, no_datas);
				showView(R.id.listview, !no_datas);
				adapter.notifyDataSetChanged();
				segView.setTextValues(new String[]{"全部("+data.getNumAll()+")","活动中("+data.getNumOngoing()+")","待开始("+data.getNumTostart()+")","已结束("+data.getNumEnd()+")"});
				pullToRefreshScrollView.onRefreshComplete();
			}
			
			@Override
			public void onFailure(int statusCode, String message) {
				pullToRefreshScrollView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<CouponActivateListResult>() {
				};
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.img_search:
				refreshData();
				break;
			default:break;
		}
		super.onClick(v);
	}
	
	@Override
	public void onSegmentViewClick(int index) {
		state = index;
		refreshData();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		intent.setClass(mActivity, CouponActivateDetailActivity.class);
		intent.putExtra("id", datas.get(position).getId());
		startActivity(intent);
	}

}
